'''
Author: 红色梧桐 940120450@qq.com
Date: 2024-05-10 16:27:30
LastEditors: 红色梧桐 940120450@qq.com
LastEditTime: 2024-07-24 21:43:06
FilePath: \myReader\applications\controllers\novel.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
from typing import Optional
from fastapi import APIRouter, Depends, Request, BackgroundTasks, Query

from applications.configs.func import render_template
from applications.configs.response import ResponseMessage
from applications.models import Session, get_session
from applications.models.novel import BookShelf, NovelMenu
from applications.services.novel import grab_save_novels, get_novel_list, inter_search_books_func, add_inter_books_func, \
    delete_book_shelf_func, get_content_data_func, read_noall_content, switching_book_source_func, show_source, update_private_func
from applications.schemas.Novel import NovelMenuSchemas, RecordNovelMenuSchemas, AddBookSchemas, NovelMenuReturnSchemas

novel_router = APIRouter(prefix="/novel", tags=["小说管理"])


@novel_router.get("/", summary='书籍首页', description="书籍首页")
def index(request: Request):
    return render_template(request, "/novel/index.html")


@novel_router.get("/list", summary="小说列表", description="小说列表")
def get_menu_html(request: Request, g: int):
    return get_novel_list(request.state.user, g)


@novel_router.get("/menus", summary="章节列表", description="章节列表")
def get_menu_data(id: int, start: Optional[int] = Query(None), end: Optional[int] = Query(None), session: Session = Depends(get_session)): # type: ignore
    data_list = session.query(NovelMenu.id, NovelMenu.name, NovelMenu.order, NovelMenu.drive_name).filter_by(novel_id=id).filter(NovelMenu.order >= start).filter(NovelMenu.order <= end).order_by("order").all()
    return [NovelMenuReturnSchemas.model_validate(data).model_dump() for data in data_list]


@novel_router.post("/content", summary="获取章节内容", description="获取章节内容")
def get_content_data(request: Request, data: RecordNovelMenuSchemas, tasks: BackgroundTasks):
    code, msg = get_content_data_func(request.state.user, data, tasks)
    return ResponseMessage(code, msg)


@novel_router.post("/grabSaveNovels", summary="更新书籍", description="更新书籍")
def get_menu_data(request: Request, data: NovelMenuSchemas, tasks: BackgroundTasks):
    code, msg = grab_save_novels(request.state.user, data, tasks)
    return ResponseMessage(code, msg)


@novel_router.get("/searchBooks", summary="查询书籍", description="查询书籍")
def search_books(request: Request, name: str, driver: str):
    code, msg = inter_search_books_func(request.state.user, name, driver)
    return ResponseMessage(code, msg)


@novel_router.post("/addBook", summary="添加书籍", description="添加书籍")
def add_book(request: Request, data: AddBookSchemas, background_tasks: BackgroundTasks):
    code, msg = add_inter_books_func(request.state.user, data, background_tasks)
    return ResponseMessage(code, msg)


@novel_router.post("/deleteBookShelf", summary="删除书架", description="删除书架")
def delete_book_shelf(request: Request, data: NovelMenuSchemas):
    code, msg = delete_book_shelf_func(request.state.user, data)
    return ResponseMessage(code, msg)


@novel_router.post("/switchingSource", summary="切换书源", description="切换书源")
def switching_book_source(request: Request, data: RecordNovelMenuSchemas, tasks: BackgroundTasks):
    code, msg = switching_book_source_func(request.state.user, data, tasks)
    return ResponseMessage(code, msg)


@novel_router.get("/source_info", summary='书源页面', description="书源页面")
def source_info(request: Request, book_id: int, chapter_order: int):
    sources, now_source = show_source(book_id, chapter_order)
    return render_template(request, "/novel/source.html", {"sources": sources, "now_source": now_source, "book_id": book_id, "chapter": chapter_order})


@novel_router.post("/read_noall", summary='朗读内容', description="朗读内容")
def read_noall_view(data: RecordNovelMenuSchemas):
    code, msg= read_noall_content(data.novel_id, data.chapter_id)
    return ResponseMessage(code, msg)


@novel_router.post("/update_private", summary='私密书籍', description="私密书籍")
def update_private_view(request: Request, data: NovelMenuSchemas):
    code, msg= update_private_func(request.state.user, data.novel_id)
    return ResponseMessage(code, msg)